/*
 * sys_env_conf.c
 *
 *  Created on: 2017年9月14日
 *      Author: 0yfj0
 */

#include "sys_env2_conf.h"

/*----------------------------------------------------------------------------*/
#if (defined(USING_QT_EMULATION))
#include <string.h>
#include <stdio.h>
static uint8_t _flash2[SYS_ENV2_SIZE * (SYS_ENV2_END - SYS_ENV2_START + 1)];
int _env2_flash_write(uint32_t addr, void *buff, uint16_t len)/* 0-成功，-1-失败 */
{
	if (addr + len > sizeof(_flash2) || !buff || !len)
	{
		printf("write2 error: [@%08d], len[%d]\r\n", addr, len);fflush(stdout);
		return -1;
	}

	printf("write2: [@%04d], len[%d]\r\n", addr, len);fflush(stdout);
	memcpy(&_flash2[addr], buff, len);
	return (0);
}
int _env2_flash_read(uint32_t addr, void *buff, uint16_t len)/* 0-成功，-1-失败 */
{
	if (addr + len > sizeof(_flash2) || !buff || !len)
	{
		printf("read2 error: [@%08d], len[%d]\r\n", addr, len);fflush(stdout);
		return -1;
	}

	printf("read2: [@%04d], len[%d]\r\n", addr, len);fflush(stdout);
	memcpy(buff, &_flash2[addr], len);
	return (0);
}
int _env2_flash_erase_sector(uint16_t sector)/* 0-成功，-1-失败 */
{
	if (sector < SYS_ENV2_START || sector > SYS_ENV2_END)
	{
		printf("erase2 error: [@%04d]\r\n", sector);fflush(stdout);
		return -1;
	}

	printf("erase2: [@%04d]\r\n", sector);fflush(stdout);
	memset(&_flash2[sector * SYS_ENV2_SIZE], 0xFF, SYS_ENV2_SIZE);
	return 0;
}
#elif defined(USING_RT_THREAD)
#include "rtthread.h"
#include "spiffs_params.h"
int _env2_flash_write(uint32_t addr, void *buff, uint16_t len)/* 0-成功，-1-失败 */
{
	rt_device_t dev;

	dev = rt_device_find(SPIFFS_DEVICE_NAME);
	if (dev != RT_NULL)
	{
		if (rt_device_write(dev, addr, buff, len) == len)
		return (0);
	}

	return (-1);
}
int _env2_flash_read(uint32_t addr, void *buff, uint16_t len)/* 0-成功，-1-失败 */
{
	rt_device_t dev;

	dev = rt_device_find(SPIFFS_DEVICE_NAME);
	if (dev != RT_NULL)
	{
		if (rt_device_read(dev, addr, buff, len) == len)
		return (0);
	}

	return (-1);
}
#elif defined(NRF51)
#include "sys.h"
int _env2_flash_write(uint32_t addr, void *buff, uint16_t len)/* 0-成功，-1-失败 */
{
	int sector;
	int off;

	sector = addr / SYS_FLASH_SIZE;
	off = addr & (SYS_FLASH_SIZE - 1);
	if (sys_flash_write(sector, off, buff, len) == len)
		return 0;
	return -1;
}
int _env2_flash_read(uint32_t addr, void *buff, uint16_t len)/* 0-成功，-1-失败 */
{
	int sector;
	int off;

	sector = addr / SYS_FLASH_SIZE;
	off = addr & (SYS_FLASH_SIZE - 1);
	if (sys_flash_read(sector, off, buff, len) == len)
		return 0;
	return -1;
}
int _env2_flash_erase_sector(uint16_t sector)/* 0-成功，-1-失败 */
{
	return sys_flash_erase((int) sector);
}

#else
#include "drv_w25q128.h"
int _env2_flash_write(uint32_t addr, void *buff, uint16_t len)/* 0-成功，-1-失败 */
{
	if (w25q128_write(addr, buff, len) == len)
	return (0);

	return (-1);
}
int _env2_flash_read(uint32_t addr, void *buff, uint16_t len)/* 0-成功，-1-失败 */
{
	if (w25q128_read(addr, buff, len) == len)
	return (0);

	return (-1);
}
#endif
/*----------------------------------------------------------------------------*/
